home *** CD-ROM | disk | FTP | other *** search
- /* CPROG16.CPP - types itself to the screen as a demo of reading a file a
- character at a time */
-
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
-
- void main(void)
- {
- FILE *file1; // Declares a file pointer
- char c; // A byte of storage to receive each incoming character
-
- if( ! ( file1=fopen("cprog16.cpp", "r") ) )
- {
- cputs("Couldn't open CPROG12.CPP\n");
- exit(0);
- }
-
- /* Tries to open CPROG16.CPP for reading. File1 gets the return value
- from fopen() - 0 for failure, or a file pointer. */
-
- while( (c=fgetc(file1)) != EOF )
- putchar( c );
-
- /* The loop runs as long as the return value from fgetc() is not EOF.
- EOF means end of file - it is a particular value which has been
- #defined as EOF. Note how the file's identifying number, stored in
- file1, is passed to fgetc() to tell it where you want it to take the
- next character from. */
-
- fclose(file1); // After closing the file, file1 is available for
- // resue with another file.
- putchar('\n');
- }
-